package com.zhang.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class SingleCsvToMultipleTables {
    public static void main(String[] args) {
        String csvFilePath = "D:\\桌面\\无标题.csv";
        try {
            generateCreateTableStatements(csvFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void generateCreateTableStatements(String csvFilePath) throws IOException {
        Map<String, List<String>> tableColumns = new HashMap<>();
        try (BufferedReader br = new BufferedReader(new FileReader(csvFilePath))) {
            String line;
            boolean isHeader = true;
            while ((line = br.readLine()) != null) {
                if (isHeader) {
                    isHeader = false;
                    continue;
                }
                String[] values = line.split("\t");
                String tableName = values[2]; 
                String columnName = values[3]; 
                String columnType = values[15]; 
                String isNullable = values[6]; 
                String columnDefault = values[5]; 
                String columnKey = values[16]; 
                String extra = values[17]; 

                StringBuilder columnDefinition = new StringBuilder();
                columnDefinition.append(columnName).append(" ").append(columnType);

                if ("NO".equals(isNullable)) {
                    columnDefinition.append(" NOT NULL");
                }

                if (!"".equals(columnDefault)) {
                    columnDefinition.append(" DEFAULT ").append(columnDefault);
                }

                if ("PRI".equals(columnKey)) {
                    columnDefinition.append(" PRIMARY KEY");
                }

                if (!"".equals(extra)) {
                    columnDefinition.append(" ").append(extra);
                }

                tableColumns.computeIfAbsent(tableName, k -> new ArrayList<>()).add(columnDefinition.toString());
            }
        }

        for (Map.Entry<String, List<String>> entry : tableColumns.entrySet()) {
            String tableName = entry.getKey();
            List<String> columns = entry.getValue();
            String createTableStatement = generateCreateTableStatement(tableName, columns);
            System.out.println("Table: " + tableName);
            System.out.println(createTableStatement);
            System.out.println();
        }
    }

    public static String generateCreateTableStatement(String tableName, List<String> columns) {
        StringBuilder createTable = new StringBuilder();
        createTable.append("CREATE TABLE ").append(tableName).append(" (\n");

        for (int i = 0; i < columns.size(); i++) {
            createTable.append("    ").append(columns.get(i));
            if (i < columns.size() - 1) {
                createTable.append(",\n");
            }
        }

        createTable.append("\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
        return createTable.toString();
    }
}    